/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StyleSettingPanel.java
 *
 * Created on Nov 8, 2010, 8:50:00 AM
 */
package dae.fxcreator.ui.usersettings;

import dae.fxcreator.io.FXSettings;
import dae.fxcreator.io.FXSingleton;
import dae.fxcreator.io.type.ShaderTypeLibrary;
import dae.fxcreator.node.IONode;
import dae.fxcreator.node.ShaderInput;
import dae.fxcreator.node.ShaderOutput;
import dae.fxcreator.node.ShaderType;
import dae.fxcreator.node.graph.JGraphNode;
import dae.fxcreator.node.gui.GradientCellRenderer;
import dae.fxcreator.node.gui.GraphGradient;
import dae.fxcreator.node.gui.NodeStyle;
import java.awt.BorderLayout;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Koen
 */
public class StyleSettingPanel extends javax.swing.JPanel implements ChangeListener {

    private IONode node;

    /** Creates new form StyleSettingPanel */
    public StyleSettingPanel(ShaderTypeLibrary library) {


        node = new IONode("Node style example", "node", library);
        ShaderInput input1 = new ShaderInput(node, "op1", "NORMAL", library.getType("FLOAT3"));
        ShaderInput input2 = new ShaderInput(node, "op2", "DIRECTION", library.getType("FLOAT3"));

        ShaderOutput output1 = new ShaderOutput(node, "result", "", library.getType("FLOAT"), null);
        node.addInput(input1);
        node.addInput(input2);
        node.addOutput(output1);

        initComponents();
        FXSingleton singleton = FXSingleton.getSingleton();
        FXSettings settings = singleton.getFXSettings();
        lstStyles.setListData(settings.getAllStylesAsList().toArray());

        cboGradient.setRenderer(new GradientCellRenderer());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        lblGradient = new javax.swing.JLabel();
        lblTitleFont = new javax.swing.JLabel();
        cboTitleFont = new javax.swing.JComboBox();
        lblPortNameFont = new javax.swing.JLabel();
        cboPortNameFont = new javax.swing.JComboBox();
        lblSemanticFont = new javax.swing.JLabel();
        cboSemanticFont = new javax.swing.JComboBox();
        jGraphNode1 = new JGraphNode(null, node);
        jScrollPane1 = new javax.swing.JScrollPane();
        lstStyles = new javax.swing.JList();
        cboGradient = new javax.swing.JComboBox();
        lblStyles = new javax.swing.JLabel();
        btnNewStyle = new javax.swing.JButton();
        lblPreview = new javax.swing.JLabel();

        lblGradient.setText("Gradient : ");

        lblTitleFont.setText("Title font : ");

        cboTitleFont.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
        cboTitleFont.addItemListener(new java.awt.event.ItemListener() {

            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboTitleFontItemStateChanged(evt);
            }
        });

        lblPortNameFont.setText("Port name font : ");

        cboPortNameFont.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
        cboPortNameFont.addItemListener(new java.awt.event.ItemListener() {

            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboPortNameFontItemStateChanged(evt);
            }
        });

        lblSemanticFont.setText("Semantic font : ");

        cboSemanticFont.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
        cboSemanticFont.addItemListener(new java.awt.event.ItemListener() {

            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboSemanticFontItemStateChanged(evt);
            }
        });

        lstStyles.setModel(new javax.swing.AbstractListModel() {

            String[] strings = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        lstStyles.addListSelectionListener(new javax.swing.event.ListSelectionListener() {

            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                lstStylesValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(lstStyles);

        cboGradient.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Item 1", "Item 2", "Item 3", "Item 4"}));
        cboGradient.addItemListener(new java.awt.event.ItemListener() {

            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboGradientItemStateChanged(evt);
            }
        });

        lblStyles.setText("Styles : ");

        btnNewStyle.setText("New Style ...");
        btnNewStyle.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewStyleActionPerformed(evt);
            }
        });

        lblPreview.setText("Preview : ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(lblStyles).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING).addComponent(btnNewStyle).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))).addGap(5, 5, 5).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(lblPortNameFont).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cboPortNameFont, 0, 273, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addComponent(lblTitleFont).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cboTitleFont, 0, 307, Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addComponent(lblSemanticFont).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cboSemanticFont, 0, 279, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addComponent(lblGradient, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(cboGradient, 0, 308, Short.MAX_VALUE)).addGroup(layout.createSequentialGroup().addComponent(lblPreview).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jGraphNode1, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE))).addContainerGap()));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(lblStyles).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(btnNewStyle)).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(lblGradient).addComponent(cboGradient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(lblTitleFont).addComponent(cboTitleFont, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(lblPortNameFont).addComponent(cboPortNameFont, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(lblSemanticFont).addComponent(cboSemanticFont, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jGraphNode1, javax.swing.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE).addComponent(lblPreview)))).addContainerGap()));
    }// </editor-fold>

    private void lstStylesValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstStylesValueChanged
        // TODO add your handling code here:
        NodeStyle style = (NodeStyle) lstStyles.getSelectedValue();
        if (style != null) {
            FXSingleton singleton = FXSingleton.getSingleton();
            FXSettings settings = singleton.getFXSettings();
            GraphGradient gg = settings.getGradient(style.getGradientName());
            cboGradient.setSelectedItem(gg);

            select(cboPortNameFont, style.getPortFontName());
            select(cboSemanticFont, style.getSemanticFontName());
            select(cboTitleFont, style.getTitleFontName());

            this.jGraphNode1.updateStyle(style);
        }
    }//GEN-LAST:event_lstStylesValueChanged

    private void select(JComboBox cbo, String item) {
        ComboBoxModel model = cbo.getModel();
        for (int i = 0; i < model.getSize(); ++i) {
            String value = model.getElementAt(i).toString();
            if (value.equals(item)) {
                cbo.setSelectedIndex(i);
            }
        }
    }

    private void cboGradientItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboGradientItemStateChanged

        NodeStyle selected = (NodeStyle) lstStyles.getSelectedValue();
        GraphGradient gg = (GraphGradient) cboGradient.getSelectedItem();
        selected.setGradientName(gg.getName());
        jGraphNode1.updateStyle(selected);
    }//GEN-LAST:event_cboGradientItemStateChanged

    private void cboTitleFontItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboTitleFontItemStateChanged

        NodeStyle selected = (NodeStyle) lstStyles.getSelectedValue();
        if (selected == null) {
            return;
        }
        selected.setTitleFontName(cboTitleFont.getSelectedItem().toString());
        jGraphNode1.updateStyle(selected);
    }//GEN-LAST:event_cboTitleFontItemStateChanged

    private void cboPortNameFontItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboPortNameFontItemStateChanged

        NodeStyle selected = (NodeStyle) lstStyles.getSelectedValue();
        if (selected == null) {
            return;
        }
        selected.setPortFontName(cboPortNameFont.getSelectedItem().toString());
        jGraphNode1.updateStyle(selected);
    }//GEN-LAST:event_cboPortNameFontItemStateChanged

    private void cboSemanticFontItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSemanticFontItemStateChanged

        NodeStyle selected = (NodeStyle) lstStyles.getSelectedValue();
        if (selected == null) {
            return;
        }
        selected.setSemanticFontName(cboSemanticFont.getSelectedItem().toString());
        jGraphNode1.updateStyle(selected);
    }//GEN-LAST:event_cboSemanticFontItemStateChanged

    private void btnNewStyleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewStyleActionPerformed
        // TODO add your handling code here:
        String styleName = JOptionPane.showInputDialog(this, "Create new style", "Style settings", JOptionPane.QUESTION_MESSAGE);
        if (styleName != null && styleName.length() > 0) {
            FXSingleton singleton = FXSingleton.getSingleton();
            FXSettings settings = singleton.getFXSettings();
            if (!settings.hasStyle(styleName)) {
                NodeStyle style = new NodeStyle(styleName);
                style.setGradientName("node");
                style.setPortFontName("portname");
                style.setSemanticFontName("semantic");
                style.setTitleFontName("title");
                settings.addStyle(style);
                lstStyles.setListData(settings.getAllStylesAsList().toArray());
            } else {
                JOptionPane.showMessageDialog(this, "Style allready exists", "Style settings", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnNewStyleActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNewStyle;
    private javax.swing.JComboBox cboGradient;
    private javax.swing.JComboBox cboPortNameFont;
    private javax.swing.JComboBox cboSemanticFont;
    private javax.swing.JComboBox cboTitleFont;
    private dae.fxcreator.node.graph.JGraphNode jGraphNode1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblGradient;
    private javax.swing.JLabel lblPortNameFont;
    private javax.swing.JLabel lblPreview;
    private javax.swing.JLabel lblSemanticFont;
    private javax.swing.JLabel lblStyles;
    private javax.swing.JLabel lblTitleFont;
    private javax.swing.JList lstStyles;
    // End of variables declaration//GEN-END:variables

    public void stateChanged(ChangeEvent e) {
        JTabbedPane pane = (JTabbedPane) e.getSource();
        if (pane.getSelectedComponent() == this) {
            FXSingleton singleton = FXSingleton.getSingleton();
            FXSettings settings = singleton.getFXSettings();
            DefaultComboBoxModel portmodel = new DefaultComboBoxModel(settings.getAllFontsAsList().toArray());
            cboPortNameFont.setModel(portmodel);
            DefaultComboBoxModel semanticmodel = new DefaultComboBoxModel(settings.getAllFontsAsList().toArray());
            cboSemanticFont.setModel(semanticmodel);
            DefaultComboBoxModel titlemodel = new DefaultComboBoxModel(settings.getAllFontsAsList().toArray());
            cboTitleFont.setModel(titlemodel);
            DefaultComboBoxModel gradientmodel = new DefaultComboBoxModel(settings.getAllGradientsAsList().toArray());
            cboGradient.setModel(gradientmodel);

            NodeStyle current = (NodeStyle) lstStyles.getSelectedValue();
            if (current != null) {
                select(cboPortNameFont, current.getPortFontName());
                select(cboSemanticFont, current.getSemanticFontName());
                select(cboTitleFont, current.getTitleFontName());
                GraphGradient gg = settings.getGradient(current.getGradientName());
                cboGradient.setSelectedItem(gg);
            }


        }
        lstStyles.setSelectedIndex(0);
    }
}
